import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-05-19
 * Time: 22:58
 */
public class Solution {
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, (o1, o2) -> o1[0] - o2[0]);
        int left = intervals[0][0];
        int right = intervals[0][1];
        ArrayList<int[]> ans = new ArrayList<>();
        for (int i = 1; i < intervals.length; i++) {
            if (right < intervals[i][0]) {
                ans.add(new int[]{left, right});
                left = intervals[i][0];
            }
            right = Math.max(right, intervals[i][1]);
        }
        ans.add(new int[]{left, right});
        // 直接将 list 转换为 int[][], 不过需要注意是二维数组
        // 所以参数是一个二位数数组
        return ans.toArray(new int[ans.size()][]);
    }
}
